package com.chengyu.core.job.portal;

import com.chengyu.core.service.ISceneRideCodeDataService;
import com.chengyu.core.util.NewRedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * 探索定时任务
 */
@Component
@Slf4j
public class RideCodeDataTask {

    public final String TASK_KEY = "hb_obtain_Ride_Code_Data";

    @Autowired
    private NewRedisUtils redisUtils;

    @Autowired
    private ISceneRideCodeDataService rideCodeDataService;

    /**
     * 定时获取获取乘车码前一天数据(凌晨一点)
     */
    @Scheduled(cron = "0 0 3,4 * * ?")
//    @Scheduled(cron = "* * * * * ?")
    public void obtainRideCodeData() {
        log.info("测试定时任务");
        if (!redisUtils.setIfAbsentAndSetExpire(TASK_KEY, "", NewRedisUtils.HOUR_ONE_EXPIRE)) {
            log.info("obtainRideCodeData 未获取锁-----退出-----定时获取获取乘车码前一天数据");
//            return;
        }
        try {
            log.info("-------------obtainRideCodeData 开始----定时获取获取乘车码前一天数据->");

            rideCodeDataService.obtainRideCodeData();

        } catch (Exception e) {
            log.error("obtainRideCodeData 定时获取获取乘车码前一天数据----失败---》",e);
            return;
        } finally {
            redisUtils.delete(TASK_KEY);
        }
        log.info("-------obtainRideCodeData 完成-----定时获取获取乘车码前一天数据-----");
    }

}
